package com.jyu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyu.reggie.common.R;
import com.jyu.reggie.entity.Category;
import com.jyu.reggie.entity.Employee;
import com.jyu.reggie.service.CategoryService;
import com.jyu.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * @description:  添加分类
     * @param category
     * @return: com.jyu.reggie.common.R<java.lang.String>
     * @date: 2023/7/27 6:54
     */
    @PostMapping
    public R<String> save(@RequestBody Category category ){
        log.info("分类添加。。。。。。");
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * @description: 分类页面分页查询
     * @param page
 * @param pageSize
     * @return: com.jyu.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     * @date: 2023/7/27 8:54
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        // 分页构造器
        Page pageInfo = new Page<>(page, pageSize);

        //条件构造器
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();

        qw.orderByDesc(Category::getSort);

        categoryService.page(pageInfo, qw);

        return R.success(pageInfo);
    }

    /**
     * @description: 删除分类
     * @param ids
     * @return: com.jyu.reggie.common.R<java.lang.String>
     * @date: 2023/7/27 8:46
     */
    @DeleteMapping
    public R<String> remove( Long ids){

//        categoryService.removeById(id);

        categoryService.remove(ids);

        return R.success("删除成功");
    }

    /**
     * @description: 修改分类信息
     * @param category
     * @return: com.jyu.reggie.common.R<java.lang.String>
     * @date: 2023/7/27 8:53
     */
    @PutMapping
    public R<String> update (@RequestBody Category category){

        log.info(category.toString());
        categoryService.updateById(category);

        return R.success("修改成功");
    }


    /**
     * @description: 分类信息列表
     * @param category
     * @return: com.jyu.reggie.common.R<java.util.List<com.jyu.reggie.entity.Category>>
     * @date: 2023/7/27 16:39
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());

        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }
}
